﻿using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Renren.Services;
using Renren.ViewModels.Page;

namespace Renren.Pages
{
    public partial class UserPage : PhoneApplicationPage
    {
        private bool firstLoaded = true;

        public UserPageViewModel ViewModel { get; set; }

        public UserPage()
        {
            InitializeComponent();

            this.ViewModel = new UserPageViewModel();
            FeedService.GetFeed(this.ViewModel.NewsList, RequestType.GetInit, null, this.ViewModel.UserId);
            DataContext = this.ViewModel;

            this.Loaded += (sender, arg) =>
                {
                    if (this.firstLoaded)
                    {
                        this.NewsStreamBox.LoadHead += (s, e) => FeedService.GetFeed(this.ViewModel.NewsList, RequestType.GetInit, e.Callback, this.ViewModel.UserId);
                        this.NewsStreamBox.LoadTail += (s, e) => FeedService.GetFeed(this.ViewModel.NewsList, RequestType.GetMore, e.Callback, this.ViewModel.UserId);

                        this.firstLoaded = false;
                    }
                };
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.ViewModel.UserId = (long)PhoneApplicationService.Current.State["UserPage"];
            UserService.GetUserProfile(this.ViewModel.UserView, this.ViewModel.UserId);
        }
    }
}